<template>
  <div>
    <el-container style="background: #f5f7f9;">
      <el-header style="background: #fff;padding: 12px 12px;height:auto;">
        <div>
          <el-form :model="queryData" inline size="small">
            <el-form-item>
              <el-input v-model="queryData.fbaCode" placeholder="请输入仓码搜索" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="query()">搜索</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-header>
      <el-main style="margin-top: 15px; background: #fff;padding: 12px 16px 0 16px;height: 100%;">
        <div style="height: 100%;margin-bottom: 12px;display: flex;">
          <el-button v-has="30101" size="small" type="primary" @click="addFbaAddress">添加FBA地址</el-button>
        </div>
        <el-table ref="multipleTable" :data="tableData" stripe border :max-height="tableHeight">
          <el-table-column prop="fbaCode" label="仓库编码" />
          <el-table-column prop="countryCode" label="国家" />
          <el-table-column prop="state" label="州/省" />
          <el-table-column prop="city" label="城市" />
          <el-table-column prop="address" label="仓库地址" />
          <el-table-column prop="zipCode" label="邮编" />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button v-has="30102" size="small" shape="circle" type="text" @click="handleEdit(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer style="padding:0;background: #fff;">
        <div style="text-align: right; padding-top: 10px;">
          <el-pagination background :current-page="queryData.currentPage" :page-sizes="[50,100]" :page-size="queryData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageTotal" style="float:right;" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </el-footer>
    </el-container>
    <el-dialog :visible.sync="dialogFormVisible" width="35%">
      <el-form ref="fbaForm" :model="formData" :rules="rules" size="small" label-position="left">
        <el-row :gutter="gutter">
          <el-col :span="24">
            <el-form-item label="仓库编码" prop="fbaCode">
              <el-input v-model="formData.fbaCode" auto-complete="off" placeholder="仓库编码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="24">
            <el-form-item label="国家" prop="countryCode">
              <el-select v-model.lazy="formData.countryCode" filterable style="width:100%">
                <el-option v-for="item in countrys" :key="item.id" :label="item.nameCn" :value="item.code" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="24">
            <el-form-item label="州/省" prop="state">
              <el-input v-model.lazy="formData.state" placeholder="州/省" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="24">
            <el-form-item label="城市" prop="city">
              <el-input v-model.lazy="formData.city" placeholder="城市" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="24">
            <el-form-item label="邮编" prop="zipCode">
              <el-input v-model.lazy="formData.zipCode" placeholder="邮编" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="gutter">
          <el-col :span="24">
            <el-form-item label="仓库地址" prop="address">
              <el-input v-model.lazy="formData.address" placeholder="街道地址" class="width80" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible=false">取 消</el-button>
        <el-button size="small" type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
    </el-card>
  </div>
</template>

<script>
import request from '@/utils/request'
import { copyProperty } from '@/utils/tools'
export default {
  components: {},
  data() {
    return {
      gutter: 100,
      queryData: {
        pageSize: 50,
        currentPage: 1,
        fbaCode: ''
      },
      tableData: [],
      formDataDefault: {
        fbaCode: '',
        contacter: 'Amazon',
        phone: '9999999999',
        countryCode: 'US',
        state: '',
        city: '',
        address: '',
        address2: '',
        company: '',
        zipCode: ''
      },
      formData: {
        id: '',
        fbaCode: '',
        contacter: 'Amazon',
        phone: '9999999999',
        countryCode: '',
        state: '',
        city: '',
        address: '',
        address2: '',
        company: '',
        zipCode: ''
      },
      countrys: [],
      rules: {
        countryCode: [{ required: true, message: '请选择国家', trigger: 'change' }],
        state: [{ required: true, message: '请输入州', trigger: 'change' }],
        city: [{ required: true, message: '请输入城市', trigger: 'change' }],
        address: [{ required: true, message: '请输入地址', trigger: 'blur' }],
        zipCode: [{ required: true, message: '请输入邮编', trigger: 'blur' }],
        fbaCode: [{ required: true, message: '请输入仓库编码', trigger: 'change' }]
      },
      pageTotal: 1, // 总条数
      dialogFormVisible: false
    }
  },
  computed: {
    tableHeight() {
      return document.documentElement.clientHeight - 300
    }
  },
  created() {
    this.query()
    this.queryCountrys()
  },
  methods: {
    query() {
      request({
        url: 'basesetting/op/fba/query',
        data: this.queryData,
        method: 'post'
      }).then(res => {
        if (res.success) {
          this.tableData = res.data.records
          this.pageTotal = res.data.total
        }
      })
    },
    queryCountrys() {
      request({
        url: '/countrys',
        method: 'get'
      }).then(res => {
        if (res.success) {
          if (res.data) {
            this.countrys = res.data
          }
        }
      })
    },
    // 编辑
    handleEdit(row) {
      copyProperty(this.formData, row)
      this.dialogFormVisible = true
    },
    addFbaAddress() {
      copyProperty(this.formData, this.formDataDefault)
      this.dialogFormVisible = true
    },

    // 添加地址
    submit(formName) {
      this.$refs.fbaForm.validate(valid => {
        if (valid) {
          request({
            url: 'basesetting/op/fba',
            method: 'post',
            data: this.formData
          }).then(res => {
            if (res.success) {
              this.dialogFormVisible = false
              this.query()
            }
          })
        }
      })
    },
    // 删除
    handleDelete(index, row) {
      this.$confirm('此操作将永久删除该FBA地址, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

      })
    },
    // 分页
    handleSizeChange(val) {
      this.queryData.pageSize = val
      this.queryData.currentPage = 1
      this.query()
    },
    handleCurrentChange(val) {
      this.queryData.currentPage = val
      this.query()
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  margin: 20px -10px
}
</style>
